# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: xguo

from coin.base.config import Config

# Okex Futures
from coin.exchange.okex_futures.order_gateway import OkexFuturesOrderGateway
from coin.exchange.okex_futures.kr_rest.futures_product import OkexFuturesProduct, OkexCurrency
from coin.exchange.okex_futures.kr_rest.public_client import OkexFuturesPublicClient
from coin.exchange.okex_futures.kr_rest.private_client import OkexFuturesPrivateClient
from coin.strategy.mm.okex_futures.feed import OkexFuturesFeedSubsystem

from coin.exchange.order_gateway.test.hk_spot_og_test_util import (
    gen_og_test_class,
    OGFixture,
)
from coin.proto.coin_query_pb2 import ProductOrderElement
import coin.proto.coin_order_enums_pb2 as coin_order


def okex_futures_fixture():
  currencies = [OkexCurrency.FromStrCurrency('XRP')]
  product = OkexFuturesProduct.FromStr('XRP-USD.QUARTER')
  config = Config.from_config_filename('okex_pilot02')
  og = OkexFuturesOrderGateway(currencies, [product], config)
  public_client = OkexFuturesPublicClient()
  private_client = OkexFuturesPrivateClient(config.key_file)

  def okex_gen_order(last_price, mode):
    qty = 1.0
    if mode == 'passive':
      sell_price = int((last_price * 1.10) * 1e2) / 1e2
      buy_price = int((last_price * 0.90) * 1e2) / 1e2
    elif mode == 'aggressive':
      sell_price = int(last_price * 1e2) / 1e2
      buy_price = int(last_price * 1e2) / 1e2
    else:
      raise ValueError('Invalid mode: %s' % mode)

    buy_order = ProductOrderElement(price=buy_price, qty=qty * 5, side=coin_order.BUY_OPEN_ORDER)
    sell_order = ProductOrderElement(price=sell_price, qty=qty, side=coin_order.SELL_OPEN_ORDER)
    return [buy_order] + [sell_order] * 5

  return OGFixture(
      currencies=currencies,
      product=product,
      config=config,
      order_gateway=og,
      market_type='Futures',
      public_client=public_client,
      private_client=private_client,
      gen_order=okex_gen_order,
      feed_subsystem_cls=OkexFuturesFeedSubsystem,
  )


TestOkexFuturesOG = gen_og_test_class(okex_futures_fixture)
